//
//  WHHeaderButtonViewCell.m
//  WHPersonalData
//
//  Created by hyf on 2023/7/20.
//  Copyright © 2023 mac. All rights reserved.
//

static int WHHeaderButtonViewCellHeight = 39;

#import "WHHeaderButtonViewCell.h"
#import "SDAutoLayout.h"
#import "WHHeaderButtonModel.h"

@interface WHHeaderButtonViewCell()
@property(nonatomic,strong)UIView *mbgView;
@property(nonatomic,strong)UIImageView *micon;
@property(strong,nonatomic)UILabel *mNameLabel;

@property (strong, nonatomic) WHHeaderButtonModel*cateModel;

@end
@implementation WHHeaderButtonViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setViewChilds];
    }
    return self;
}

-(void) setViewChilds{
    UIView *bgView =[[UIView alloc] init];
    _mbgView = bgView;
    [self.contentView addSubview:_mbgView];
    
    UIImageView *img = [[UIImageView alloc] init];
    img.contentMode = UIViewContentModeScaleAspectFit;
    _micon = img;
    [_mbgView addSubview:_micon];
    
    UILabel *name = [[UILabel alloc] init];
    name.textAlignment = NSTextAlignmentCenter;
    name.font = [UIFont systemFontOfSize:12];
    name.numberOfLines = 1;
    _mNameLabel = name;
    [_mbgView addSubview:_mNameLabel];
    
}

-(void) setViewChildsAttr{
    int imgH = self.cateModel.imgH;
    if(!imgH){
        imgH = 35;
    }
    int imgw = self.cateModel.imgW;
    if(!imgw){
        imgw = 35;
    }
    
    _mbgView
    .sd_layout
    .topSpaceToView(self.contentView, 0)
    .bottomSpaceToView(self.contentView, 0)
    .leftSpaceToView(self.contentView, 0)
    .rightSpaceToView(self.contentView, 0);
    
    _micon
    .sd_layout
    .centerXEqualToView(_mbgView)
    .topSpaceToView(_mbgView, 15)
    .heightIs(imgH)
    .widthIs(imgw);

    _mNameLabel
    .sd_layout
    .topSpaceToView(_micon, 5)
    .bottomSpaceToView(_mbgView, 0)
    .leftSpaceToView(_mbgView, 0)
    .rightSpaceToView(_mbgView, 0);

}

- (CABasicAnimation *)getLargeAnimation {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    //速度控制函数，控制动画运行的节奏
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration    = 0.1;       //执行时间
    animation.repeatCount = 1;      //执行次数
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;           //保证动画效果延续
    animation.fromValue = [NSNumber numberWithFloat:1.0];   //初始伸缩倍数
    animation.toValue = [NSNumber numberWithFloat:1.25];
    return animation;
}

- (void)setCateModel:(WHHeaderButtonModel *)cateModel{
    _cateModel = cateModel;
    [self setViewChildsAttr];

    if(cateModel.isSel){
        UIImage * myImage = [UIImage imageNamed:cateModel.selImg];
        self.micon.image = myImage;
        if(cateModel.selColor){
            self.mNameLabel.textColor = cateModel.selColor;
        }else{
            self.mNameLabel.textColor = RGBColor(28,187,240);
        }
        
        if(cateModel.isEnlargeSelImg){
            //放大效果
            CABasicAnimation * animation = [self getLargeAnimation];
            //结束伸缩倍数
            [[self.micon layer] addAnimation:animation forKey:cateModel.code];
        }
    }else{
        UIImage * myImage = [UIImage imageNamed:cateModel.noSelImg];
        self.micon.image = myImage;
        if(cateModel.noSelColor){
            self.mNameLabel.textColor = cateModel.noSelColor;
        }else{
            self.mNameLabel.textColor = RGBColor(221, 218, 218);
        }
        if(cateModel.isEnlargeSelImg){
            [[self.micon layer] removeAnimationForKey:cateModel.code];
        }
    }
    
    if(cateModel.font)
    {
        self.mNameLabel.font = cateModel.font;
    }else{
        self.mNameLabel.font = [UIFont systemFontOfSize:12];
    }
    self.mNameLabel.text = cateModel.title;
}

@end
